import os

import requests as req

from goodDesignImage2018Spider.util.Logger import Logger
from goodDesignImage2018Spider.util.mysql_opt import mysql_opt

base_path = "F:/goodDesign/"


class downloadGoodDesignImgJob(object):
    def __init__(self):
        self.logger = Logger()

    def download_record(self):
        conn, cursor = mysql_opt.get_conn()
        query_sql = 'SELECT * FROM `robot_good_design_img_record` where download_status=0 limit 6;'
        cursor.execute(query_sql)
        rets = self.dict_fetchall(cursor)
        if not rets:
            self.logger.error("下完啦！！！")
            return
        for ret in rets:
            self.down_img(ret)
            self.update_downloaded(ret, cursor)

        conn.commit()
        cursor.close()
        conn.close()

    def dict_fetchall(self, cursor):
        # "Return all rows from a cursor as a dict"
        columns = [col[0] for col in cursor.description]
        return [
            dict(zip(columns, row))
            for row in cursor.fetchall()
        ]

    # 下载
    def down_img(self, ret):
        record_id = ret['id']
        award_id = ret['award_id']
        award_code = ret['award_code']
        img_download_url = ret['img_download_url']
        year = ret['year']
        award_type = ret['award_type']
        img_file_name = ret['img_file_name']

        # 创建父级文件夹
        parent_path = base_path + year + "/" + award_type
        if not os.path.exists(parent_path):
            os.makedirs(parent_path)
        #  下载
        response = req.get(img_download_url)
        with open(parent_path + "/" + img_file_name, 'wb') as f:
            f.write(response.content)

    # 更新下载状态
    def update_downloaded(self, ret, cursor):
        vals = (ret['id'])
        update_sql = 'update robot_good_design_img_record set download_status=1 where id = %s;'
        if vals:
            cursor.execute(update_sql, vals)
